package com.mkyong.common.controller;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mkyong.common.domain.Department;
import com.mkyong.common.domain.Employee;

@Controller
@RequestMapping("/welcome")
public class HelloController {

	//@PersistenceContext
	//private  EntityManager entityManager;
	
	
	
	@RequestMapping(method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {

		
		HelloController test = new HelloController();
		
		//EntityTransaction tx = entityManager.getTransaction();
		//tx.begin();
		try {
			//test.createEmployees();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//tx.commit();

		//test.listEmployees();
		
		model.addAttribute("message", "Spring 3 MVC Hello World");
		return "hello";

	}
	
	
//	private void createEmployees() {
//		int numOfEmployees = entityManager.createQuery("Select a From Employee a").getResultList().size();
//		if (numOfEmployees == 0) {
//			Department department = new Department("java");
//			entityManager.persist(department);
//
//			entityManager.persist(new Employee("Jakab Gipsz",department));
//			entityManager.persist(new Employee("Captain Nemo",department));
//
//		}
//	}
//
//	@SuppressWarnings("unchecked")
//	private void listEmployees() {
//		
//		List<Employee> resultList = entityManager.createQuery("Select a From Employee a").getResultList();
//		System.out.println("num of employess:" + resultList.size());
//		for (Employee next : resultList) {
//			System.out.println("next employee: " + next);
//		}
//	}
	
}